package com.ruitu.health_1.ui;

import android.os.Bundle;
import android.os.Vibrator;
import android.widget.ImageView;

import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.health_1.R;
import com.ruitu.router_module.Logg;
import com.ruitu.router_module.util.ZXingUtil;

import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView;

public class ScanActivity extends BaseActivity implements QRCodeView.Delegate {
    private QRCodeView zx_scan;
    private ImageView iv_qrcode;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_scan;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        zx_scan = findViewById(R.id.zx_scan);
        iv_qrcode = findViewById(R.id.iv_qrcode);
        zx_scan.setDelegate(this);
        iv_qrcode.setImageBitmap(ZXingUtil.createQRCode("2046",400,400,null));
    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(100);
    }


    @Override
    public void onScanQRCodeSuccess(String result) {
        vibrate();
        Logg.i("result: " + result);
    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        ToastUtils.showShort("扫码错误");

    }

    @Override
    protected void onStart() {
        super.onStart();
        zx_scan.startCamera();
        zx_scan.startSpot();
    }

    @Override
    protected void onResume() {
        super.onResume();
        zx_scan.showScanRect();
    }

    @Override
    protected void onStop() {
        zx_scan.stopCamera();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        zx_scan.onDestroy();
        super.onDestroy();
    }
}
